var htmlD='您好，您的问题我现在还没办法回答，不过请给我一些学习的时间，之后我会提供更好的服务。';
var dataInit={id:1,ans:htmlD};
$.ajaxSetup({
    cache: false
});
$(document).ready(function () {
    $("#redirect-url").hide();
    //定位到问题输入框
    $("#MSG").focus();
    // 防止自动提交
    $("#MSG").keypress(function (e) {
        if ((e.which || e.keyCode) == 13) {
            return false;
        }
    });
    // 上翻下翻
    $("#MSG").keyup(function (e) {
        if ((e.which || e.keyCode) == 13) {
            exec();
        } else if ((e.which || e.keyCode) == 38) {
            $('input:text').val(info.up());
        } else if ((e.which || e.keyCode) == 40) {
            $('input:text').val(info.down());
        }
    });

    $(function () {
        $(".conter1").niceScroll({
            cursorcolor: "#666666",
            cursoropacitymax: 1,
            touchbehavior: false,
            cursorwidth: "5px",
            cursorborder: "0",
            cursorborderradius: "5px",
            horizrailenabled: false
        });
    });
    var cache = {};
    $("#MSG").autocomplete({
        source: function (request, response) {
            var term = request.term;
            if (term in cache) {
                response(cache[term]);
                return;
            }

            var inputLength = $("#MSG").val().trim().length;
            var surplusLength = 100 - inputLength;
            if (surplusLength > 0) {
                $("#tx-numbers").html(surplusLength);
            } else {
                $("#tx-numbers").html("0");
            }


            var str = filterSpecialStr(term).trim();
            if (str != "") {
                $.post('/ckm-qa/web/autocomplete;jsessionid=F82BA626212B7EB7F4314B8EBB99B9D1',
                    {term: term},
                    function (data, textStatus, jqXHR2) {
                        cache[term] = data;
                        response(data);
                        $("MSG").blur();
                    },
                    'json');
            }
        },
        minLength: 2,
        position: { my: "left bottom", at: "left top" }

    });
    $(".fh").click(function () {
        $(".zj01").animate({left: "100%"}, function () {
            $("#redirect-url").hide();
            $(".zj").show();
        });
    });
});
var dataListObject;
var questionKeyword;
//问题提问
function exec() {
    questionKeyword = $("#MSG").val();
    var wordLength = questionKeyword.trim().length;
    if (wordLength > 100) {
        alert("问题字数:" + wordLength + ",您的问题字数有点多哦！");
        return;
    }
    category = "";
    $("#tx-numbers").html(100);

    if (questionKeyword == '' || questionKeyword == null || questionKeyword.trim().length == 0) {
        alert("您没有给我想要的问题哦！");
        return;
    }
    questionKeyword = escapeHTML(questionKeyword);
    var str = filterSpecialStr(questionKeyword);
    if (str.substr(0, 1) == "[") {
        alert("您的问题中包含我识别不了的特殊字符" + str + "哦！");
        return;
    }
    fillQuestionsArea();
    $("#MSG").val("");
    $("#MSG").blur();
    if (questionKeyword == "预约预审") {
//        预设的问题，返回预设列表
        askPredict(questionKeyword);
    } else {
        //向ckm发送请求
        askResultToCKM(questionKeyword, false);
    }
    $("#MSG").focus();
}
//将问题添加到聊天窗口
function fillQuestionsArea() {
    var youWantToknow = template("xiaojihuida-zn-wen", {keyword: questionKeyword});
    $("#style_2").append(youWantToknow);
}

//返回问题的答案
var category = "";
function askResultToCKM(question, isSearch, obj) {
    $(".zj01").animate({left: "100%"}, function () {
        $("#redirect-url").hide();
        $(".zj").show();
    });
    //从全文检索数据库TRS Database Server中检索数据
    $.post('',
        {term: question, category: category},
        function (data, textStatus, jqXHR2) {
            leftAnswerPageFill(data);
        }
        , 'json');
    leftAnswerPageFill(dataInit);
}
//返回预设回答。
function askPredict(question) {
//        $.post('', { question: question }, function (data, textStatus, jqXHR2) {
//            if (textStatus != 'success' || data == null) {
//                return;
//            }
//            $("#MSG").val('');
//            leftAnswerPageFill(data);
//        }, 'json');
    leftAnswerPageFillDefault();
    return;
}
//答案添加到聊天窗口，并滚动滚动条
function leftAnswerPageFill(data) {
    var ans = data.ans;
    var html = template("xiaojihuidaTchd", {id: data.id});
    $("#style_2").append(html);
    var scrollArea = $("#style_2").children(".zn_da:last-child");
    var ansArea = $("#style_2").children(".zn_da:last-child").children(".text").children(".ans").children(".answer");
    $(ansArea).append(ans);
    $("#style_2").animate({ scrollTop: $("#style_2").scrollTop() + $(scrollArea).offset().top - $("#style_2").offset().top}, 2000);
}
//默认答案添加到聊天窗口，并滚动滚动条
function leftAnswerPageFillDefault() {
//        var ans = data.ans;
    var html = template("defaultAnswer");
    $("#style_2").append(html);
    var scrollArea = $("#style_2").children(".zn_da:last-child");
    var ansArea = $("#style_2").children(".zn_da:last-child").children(".text").children(".ans").children(".answer");
//        $(ansArea).append(ans);
    $("#style_2").animate({ scrollTop: $("#style_2").scrollTop() + $(scrollArea).offset().top - $("#style_2").offset().top}, 1000);
}
//去除特殊字符
function filterSpecialStr(str) {
    var pattern = new RegExp("[`~!@#$^&*()=|{}':;',%\\[\\].<>/?~@#￥……&*（）——|{}【】‘”“'、]");
    var rs = "";
    var specialChar = "";
    for (var i = 0; i < str.length; i++) {
        rs = rs + str.substr(i, 1).replace(pattern, '');
    }
    specialChar = str.match(pattern);
    if (specialChar != null) {
        return "[" + specialChar + "]";
    }
    /*     if(str.length != rs.length){
     return "";
     } */
    return rs;
}
function send2() {
    $("#MSG").val("预约预审");
    exec();
}
//防注入
function escapeHTML(val) {
    val = val.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return val;
}
